#include "libpocketj.hpp"
#include <fstream>
#include <stdint.h>
#include <cstdarg>
#include <cstdlib>
#include <fcntl.h> //stat stat_buf

namespace pocketj {

  void printUsage(const std::string& fileName)
  {
    std::cerr << fileName << " : the command line utility for pocketj"
              << std::endl;
    std::cerr << std::endl;
    std::cerr << "usage: " << fileName << " [options]" << std::endl << std::endl;
    std::cerr << "\t-t, --threshold=T\tspecify the threshold." << std::endl;
    exit(1);
  }

  void procArgs(int argc, char** argv, double& threshold, std::string& input)
  {
    std::string fileName = argv[0];
    for (int i = 1; i < argc; i++) {
      std::string argBuf = argv[i];
      if(argBuf == "-h" || argBuf == "--help") {
        pocketj::printUsage(fileName);
      } else if (argBuf == "-t") {
        threshold = std::atof(argv[++i]);
      } else if ((argBuf.find("--threshold=")) != std::string::npos) {
        unsigned int index = argBuf.find("=");
        threshold = std::atof(argv[i] + index + 1);
      } else if (argBuf == "-i") {
        input = argv[++i];
      } else if ((argBuf.find("--input=")) != std::string::npos) {
        unsigned int index = argBuf.find("=");
        input = std::atof(argv[i] + index + 1);
      } else {
        std::cerr << "check this argument: " << argv[i] << std::endl;
        exit(1);
      }
    }
  }
}


int main(int argc, char** argv)
{
  double threshold = 0.85;
  std::string input;
  pocketj::procArgs(argc, argv, threshold, input);
  //pocketj::Analyzer anal = new pocketj::Analyzer(threshold, depth);
  pocketj::Analyzer anal(threshold, 1, "./db/");
  std::string line;
  int cnt = 0;

  // create save doc dbm
  std::string dbPath = "./db/doc.hdb";
  TCHDB* docHdb = tchdbnew();
  tchdbopen(docHdb, dbPath.c_str(), HDBOWRITER|HDBOCREAT|HDBOTRUNC);

  while(std::getline(std::cin, line)) {
    std::vector<std::string> rv = pocketj::util::split(line, "\t");
    if (rv[1].size() < 1) continue;
    anal.addDoc(std::atoi(rv[0].c_str()), rv[1]);
    tchdbput2(docHdb, rv[0].c_str(), rv[1].c_str());
    cnt++;
    //std::cout << cnt << std::endl;
  }
  std::cout << "finish add doc" << std::endl;

  /*
  anal.addDoc(1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  anal.addDoc(2, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  anal.addDoc(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  anal.addDoc(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  anal.addDoc(5, "aaaaaaaaaaaaaaaaaaaabnsnbdnfbdsfkljdskjflk");
  */


  //anal.itrdocSave();
  anal.sortDoc();
  std::cout << "finish sort doc" << std::endl;
  std::map<int, std::set<int> > result;
  anal.doJoin(result);
  std::cout << "finish dojoin" << std::endl;

  std::map<int, std::set<int> >::iterator itr;
  for (itr = result.begin(); itr != result.end(); itr++) {
    std::set<int>::iterator itr2 = itr->second.begin();
    const int hoge = itr->first;
    char* idBuf    = tcsprintf("%d", hoge);
    char* docBuf   = tchdbget2(docHdb, idBuf);
    std::cout << hoge << "\t" << docBuf << std::endl;
    free(docBuf);
    free(idBuf);
    while (itr2 != itr->second.end()) {
      char* idBuf = tcsprintf("%d", *itr2);
      char* docBuf = tchdbget2(docHdb, idBuf);
      std::cout << *itr2 << "|2\t"<<docBuf << std::endl;
      free(docBuf);
      free(idBuf);
      itr2++;
    }

    /*
    std::cout << itr->first << " | ";
    while (itr2 != itr->second.end()) {
      std::cout << *itr2 << " | ";
      itr2++;
    }
    */
    std::cout << "\n===========\n" << std::endl;
  }
  tchdbclose(docHdb);
  tchdbdel(docHdb);


  //anal.itrIndex();
  //  anal.checkQueue();
  //std::cout << "[1]" << std::endl;
  //anal.itrdocSave();
  //anal.addDoc(1, text2);
  //anal.printParams();
}

